﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.EntityFrameworkCore;
using car.Models;

namespace car.Controllers
{
    public class TripController : Controller
    {
        public IActionResult Index()
        {
            try
            {
                using (var db = new carContext())
                {
                    return View(db.Trip.ToList());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        public IActionResult Create()
        {
            return View();
        }
        public IActionResult Created()
        {
            Trip trip = new Trip();
            string tid = Request.Form["tid"];
            string vid = Request.Form["vid"];
            string begin = Request.Form["begin"];
            string end = Request.Form["end"];
            TimeSpan t = TimeSpan.Parse(Request.Form["time"]);
            float tprice = float.Parse(Request.Form["tprice"]);
            int tnum = int.Parse(Request.Form["tnum"]);
            try
            {
                using (var db = new carContext())
                {
                    trip = db.Trip.Find(tid);
                    if (trip==null)
                    {
                        trip = new Trip();
                        trip.Tid = tid;
                    }
                    trip.Tid = tid;
                    trip.Vid = vid;
                    trip.Begin = begin;
                    trip.End = end;
                    trip.Time = t;
                    trip.Tprice = tprice;
                    trip.Tnum = tnum;
                    db.Trip.Add(trip);
                    db.SaveChanges(true);
                    ViewData["Flag"] = "true";
                }

            }
            catch (Exception)
            {
                throw;
            }
            return View("Create");
        }
        public IActionResult OrderCreate(string id)
        {
            try
            {
                using (var db=new carContext())
                {
                    string oid = Guid.NewGuid().ToString();
                    string uid = HomeController.UserUid;
                    string tid = id;
                    Order order = new Order();
                    Trip trip = db.Trip.Find(tid);
                    if(trip==null)
                    {
                        ViewData["Message"] = false;
                        return View("UserIndex");
                    }
                    else
                    {
                        if(trip.Tnum>0)
                        {
                            trip.Tnum--;
                            db.Trip.Update(trip);
                        }
                        else
                        {
                            ViewData["Message"] = false;
                            return View("UserIndex");
                        }
                    }
                    order.Oid = oid;
                    order.Uid = uid;
                    order.Tid = tid;
                    db.Order.Add(order);
                    db.SaveChanges();
                    ViewData["Message"] = true;
                }
            }
            catch(Exception)
            {
                throw;
            }

            return View("UserIndex");
        }
        [HttpGet]
        public IActionResult Delete(string tid)
        {
            try
            {
                using var db = new carContext();
                var trip = db.Trip.Find(tid);
                if (trip != null)
                {
                    db.Trip.Remove(trip);
                    ViewData["Flag"] = true;
                }
                else
                {
                    ViewData["Flag"] = false;
                }
                db.SaveChanges();
                return View("Index", db.Trip.ToList());
            }
            catch
            {
                ViewData["Flag"] = false;
            }
            return View("Index");
        }
        public IActionResult Update(string id)
        {
            ViewData.Clear();
            ViewData["Tid"] = id;
            return View();
        }
        public IActionResult Updated()
        {
            Trip trip = new Trip();
            string tid = Request.Form["tid"];
            string vid = Request.Form["vid"];
            string begin = Request.Form["begin"];
            string end = Request.Form["end"];
            TimeSpan t = TimeSpan.Parse(Request.Form["time"]);
            float tprice = float.Parse(Request.Form["tprice"]);
            int tnum = int.Parse(Request.Form["tnum"]);
            try
            {
                using (var db = new carContext())
                {
                    trip = db.Trip.Find(tid);
                    if (tid == null)
                    {
                        trip = new Trip();
                        trip.Tid = tid;
                        trip.Tid = tid;
                        trip.Vid = vid;
                        trip.Begin = begin;
                        trip.End = end;
                        trip.Time = t;
                        trip.Tprice = tprice;
                        trip.Tnum = tnum;
                        db.Trip.Add(trip);
                    }
                    else
                    {
                    trip.Tid = tid;
                    trip.Vid = vid;
                    trip.Begin = begin;
                    trip.End = end;
                    trip.Time = t;
                    trip.Tprice = tprice;
                    trip.Tnum = tnum;
                    db.Trip.Update(trip);
                    }

                    db.SaveChanges(true);
                    ViewData["Flag"] = "true";

                }
            }
            catch(Exception)
            {
                ViewData["Flag"] = "false";
            }
            
            return View("Create");
        }
        public IActionResult UserIndex()
        {
            try
            {
                using (var db = new carContext())
                {
                    return View(db.Trip.ToList());
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        [HttpPost]
        public IActionResult SearchID(string name)
        {
            var subscribelist = new carContext();
            var filelist = subscribelist.Trip.ToList();
            if (!string.IsNullOrEmpty(name))
                filelist = filelist.Where(p => p.Tid.Contains(name)).ToList();
            return View("Index", filelist);
        }
    }
}
